package com.newasia.management.controller;

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.ISysRoleService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dto.user.role.*;
import com.newasia.management.form.SysRoleForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/management")
public class RoleController {

    @Reference(timeout = 3000)
    ISysRoleService sysRoleService;

    @GetMapping("/role")
    public ResponseData selectSysMenuList(SysRoleForm form){
        SysRoleListRequset requset = new SysRoleListRequset();
        requset.setRoleId(form.getRoleId());
        SysRoleListResponse response = sysRoleService.selectSysRoleList(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @PutMapping("/role/{rid}")
    public ResponseData updateSysRole(@RequestBody SysRoleForm form,@PathVariable("rid") Long rid){
        UpdateSysRoleRequset requset = new UpdateSysRoleRequset();
        requset.setRoleId(rid);
        requset.setRoleName(form.getRoleName());
        requset.setMenuIds(form.getMenuIds());
        UpdateSysRoleResponse response = sysRoleService.updateSysRole(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @PostMapping("/role")
    public ResponseData insertSysRole(@RequestBody SysRoleForm form){
        InsertSysRoleRequset requset = new InsertSysRoleRequset();
        requset.setRoleName(form.getRoleName());
        requset.setMenuIds(form.getMenuIds());
        InsertSysRoleResponse response = sysRoleService.insertSysRole(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @DeleteMapping("/role/{rid}")
    public ResponseData deleteSysRole(@PathVariable("rid") Long rid){
        DeleteSysRoleRequset requset = new DeleteSysRoleRequset();
        requset.setRoleId(rid);
        DeleteSysRoleResponse response = sysRoleService.deleteSysRole(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }
}
